﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.EnhancedEntities;
using MovieRental_Presentation.Helpers;
using System.Collections.ObjectModel;

namespace MovieRental_Presentation.DataViews
{
    public class MoviesDataView : BaseDataView
    {
        private string _searchString;
        private ObservableCollection<Movie> _moviesList = new ObservableCollection<Movie>();
        private ObservableCollection<Movie> _topMoviesList = new ObservableCollection<Movie>();
        private ObservableCollection<Movie> _bottomMoviesList = new ObservableCollection<Movie>();

        public MoviesDataView()
        {
            ComponentManager.Instance.MovieComponent.SourceUpdated += MakeEventHandlerForProperty("MoviesList");
        }

        public ObservableCollection<Movie> MoviesList
        {
            get
            {
                var query = ComponentManager.Instance.MovieComponent.SearchMoviesByTitle(SearchString).Select(x => new Movie(x));
                UpdateObservableCollection<Movie>(_moviesList, query);
                return _moviesList;
            }
        }

        public ObservableCollection<Movie> TopMovies
        {
            get
            {
                var query = ComponentManager.Instance.MovieComponent.GetMostPopularMovies(10).Select(x => new Movie(x));
                UpdateObservableCollection<Movie>(_topMoviesList, query);
                return _topMoviesList;
            }
        }

        public ObservableCollection<Movie> BottomMovies
        {
            get
            {
                var query = ComponentManager.Instance.MovieComponent.GetNotPopularMovies(10).Select(x => new Movie(x));
                UpdateObservableCollection<Movie>(_bottomMoviesList, query);
                return _bottomMoviesList;
            }
        }

        public string SearchString
        {
            set
            {
                SetProperty<string>(ref _searchString, value, "SearchString");
                OnPropertyChanged("MoviesList");
            }
            get
            {
                return _searchString;
            }
        }
    }
}
